package com.brainhealth.customer.service.impl;

import java.util.Date;
import java.util.List;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.Learn;
import com.brainhealth.customer.domain.LearnCategory;
import com.brainhealth.customer.mapper.LearnCategoryMapper;
import com.brainhealth.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.CustomerCollectMapper;
import com.brainhealth.customer.domain.CustomerCollect;
import com.brainhealth.customer.service.ICustomerCollectService;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员收藏Service业务层处理
 * 
 * @author wolong
 * @date 2023-05-10
 */
@Service
public class CustomerCollectServiceImpl implements ICustomerCollectService 
{
    @Autowired
    private CustomerCollectMapper customerCollectMapper;
    @Autowired
    private LearnCategoryMapper learnCategoryMapper;

    /**
     * 查询会员收藏
     * 
     * @param customerCollectId 会员收藏主键
     * @return 会员收藏
     */
    @Override
    public CustomerCollect selectCustomerCollectByCustomerCollectId(Long customerCollectId)
    {
        return customerCollectMapper.selectCustomerCollectByCustomerCollectId(customerCollectId);
    }

    /**
     * 查询会员收藏列表
     * 
     * @param customerCollect 会员收藏
     * @return 会员收藏
     */
    @Override
    public List<CustomerCollect> selectCustomerCollectList(CustomerCollect customerCollect)
    {
        return customerCollectMapper.selectCustomerCollectList(customerCollect);
    }

    /**
     * 新增会员收藏
     * 
     * @param customerCollect 会员收藏
     * @return 结果
     */
    @Override
    public int insertCustomerCollect(CustomerCollect customerCollect)
    {
        customerCollect.setCreateTime(DateUtils.getNowDate());
        return customerCollectMapper.insertCustomerCollect(customerCollect);
    }

    /**
     * 修改会员收藏
     * 
     * @param customerCollect 会员收藏
     * @return 结果
     */
    @Override
    public int updateCustomerCollect(CustomerCollect customerCollect)
    {
        customerCollect.setUpdateTime(DateUtils.getNowDate());
        return customerCollectMapper.updateCustomerCollect(customerCollect);
    }

    /**
     * 批量删除会员收藏
     * 
     * @param customerCollectIds 需要删除的会员收藏主键
     * @return 结果
     */
    @Override
    public int deleteCustomerCollectByCustomerCollectIds(Long[] customerCollectIds)
    {
        return customerCollectMapper.deleteCustomerCollectByCustomerCollectIds(customerCollectIds);
    }

    /**
     * 删除会员收藏信息
     * 
     * @param customerCollectId 会员收藏主键
     * @return 结果
     */
    @Override
    public int deleteCustomerCollectByCustomerCollectId(Long customerCollectId)
    {
        return customerCollectMapper.deleteCustomerCollectByCustomerCollectId(customerCollectId);
    }

    @Override
    public boolean collectLearn(HttpServletRequest request, Learn learn)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        CustomerCollect queryCustomerCollect = new CustomerCollect();
        queryCustomerCollect.setCustomerId(customer.getCustomerId());
        queryCustomerCollect.setLearnId(learn.getLearnId());
        List<CustomerCollect> lstCustomerCollect = selectCustomerCollectList(queryCustomerCollect);
        LearnCategory learnCategory = learnCategoryMapper.selectLearnCategoryByLearnCategoryId(learn.getLearnCategoryId());
        if (lstCustomerCollect.size() == 0)
        {
            CustomerCollect collect = new CustomerCollect();
            collect.setLearnId(learn.getLearnId());
            collect.setLearnName(learn.getName());
            collect.setLearnCategoryId(learn.getLearnCategoryId());
            collect.setCategoryType(learnCategory.getCategoryType());
            collect.setCustomerId(customer.getCustomerId());
            collect.setDescription(learn.getDescription());
            collect.setPictureUrl(learn.getPictureUrl());
            collect.setCreateTime(new Date());
            insertCustomerCollect(collect);
            return true;
        } else
        {
            deleteCustomerCollectByCustomerCollectId(lstCustomerCollect.get(0).getCustomerCollectId());
            return false;
        }
    }

    @Override
    public List<CustomerCollect> getCollectList(HttpServletRequest request)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        CustomerCollect query = new CustomerCollect();
        query.setCustomerId(customer.getCustomerId());
        List<CustomerCollect> lstCustomerCollect = customerCollectMapper.selectCustomerCollectList(query);
        return lstCustomerCollect;
    }
}
